﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model;
using BLL;
using Common;
using System.Configuration;

namespace WebPortal.Controllers
{
    public class FunctionController : BaseController
    {
        #region 构造函数

        #endregion

        #region List
        public ActionResult List()
        {
            return View();
        }

        public ActionResult GetList()
        {
            string funName = Request.Params["funName"];
            int funType = int.Parse(Request.Params["funType"]);
            string actionType = Request.Params["actionType"];
            int totalCount;

            string order = Request.Params["order"];
            string sort = Request.Params["sort"];
            int pageSize = int.Parse(Request.Params["rows"]);
            int pageIndex = int.Parse(Request.Params["page"]);

            if (string.IsNullOrEmpty(sort))
            {
                sort = "FunName";
            }
            if (string.IsNullOrEmpty(order))
            {
                order = "asc";
            }
            LoginInfo loginInfos = base.loginInfo();
            FunctionBll funBll = new FunctionBll(loginInfos);
            var list = funBll.GetList(funName, funType, actionType, out totalCount);
            //if (order == "asc")
            //{
            //    list = list.OrderBy(p => sort).ToList();
            //}
            //else
            //{
            //    list = list.OrderByDescending(p => sort).ToList();
            //}
            if (pageSize != 0 && pageIndex > -1 && pageSize > -1)
            {
                list = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            }
            var jsonDict = new Dictionary<String, Object> { { "total", totalCount }, { "rows", list } };
            return Json(jsonDict);
        }

        public ActionResult Delete(string funID)
        {
            LoginInfo loginInfos = base.loginInfo();
            FunctionBll funBll = new FunctionBll(loginInfos);
            try
            {
                int result = funBll.Delete(funID);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(ex.Message);
            }
        }

        #endregion

        #region Edit
        public ActionResult Edit(string funID)
        {
            FunctionInfo funInfo = null;
            if (!string.IsNullOrEmpty(funID))
            {
                LoginInfo loginInfos = base.loginInfo();
                FunctionBll funBll = new FunctionBll(loginInfos);
                funInfo = funBll.GetInfoById(funID);
            }
            if (funInfo == null)
                funInfo = new FunctionInfo();
            return View(funInfo);
        }

        public ActionResult Update(FunctionInfo model, FormCollection collection)
        {
            #region 变量信息
            LoginInfo loginInfos = base.loginInfo();
            //loginInfos.DBName = dbName;
            int resultCount = 0;
            FunctionBll funBll = new FunctionBll(loginInfos);

            #endregion

            #region 参数检查
            if (string.IsNullOrEmpty(model.FunName))
            {
                return Json(new
                {
                    Success = false,
                    Message = "函数名称不不能为空！"
                });
            }
            #endregion

            #region 更新数据

            var taskResult = 0;
            if (string.IsNullOrEmpty(model.Guid))
            {
                model.Guid = Guid.NewGuid().ToString();
                var _tempList = funBll.GetByNameWithOutGuid(model.FunName);
                if (_tempList.Count > 0)
                {
                    return Json(new
                    {
                        Success = false,
                        Message = "保存失败，函数名称重复！"
                    });
                }
                taskResult = funBll.Insert(model);
            }
            else
            {
                var _tempList = funBll.GetByNameWithOutGuid(model.FunName, model.Guid);
                if (_tempList.Count > 0)
                {
                    return Json(new
                    {
                        Success = false,
                        Message = "保存失败，函数名称重复！"
                    });
                }
                taskResult = funBll.Update(model);
            }

            #endregion

            #region 输出结果
            if (taskResult == 1)
            {
                return Json(new
                {
                    Success = true,
                    Message = "保存成功"
                });
            }
            else
            {
                return Json(new
                {
                    Success = false,
                    Message = "保存失败"
                });
            }

            #endregion
        }


        #endregion

        public ActionResult Select()
        {
            return View();
        }
        public ActionResult GetDataType()
        {
            string value = Request.Params["Value"];
            if (value.IndexOf("[") > -1)
            { //从源表中去一个值

            }
            LoginInfo loginInfos = base.loginInfo();
            FunctionBll funBll = new FunctionBll(loginInfos);
            JsonResponse<string> jsonResponse = new JsonResponse<string>();
            try
            {
                string tp = funBll.GetDataType(value);
                jsonResponse.Success = true;
                jsonResponse.Message = tp;
            }
            catch (Exception ex)
            {
                jsonResponse.Success = false;
                jsonResponse.Message = ex.Message;

            }
            return Json(jsonResponse);
        }
    }
}
